﻿using BCCommon;
using BCEntity.TMS.VehicleIgnoreOrderAssignRecord;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCData.TMS.VehicleIgnoreOrderAssignRecord
{
    public class VehicleIgnoreOrderAssignRecordData : IVehicleIgnoreOrderAssignRecordData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT VehicleIgnoreOrderAssignRecordId,OrderId,OrderNo,VehicleNo,DriverId,CreateTime 
                                            FROM VehicleIgnoreOrderAssignRecord";
        public VehicleIgnoreOrderAssignRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        public async Task<VehicleIgnoreOrderAssignRecordEntity> Add(VehicleIgnoreOrderAssignRecordEntity vehicleIgnoreOrderAssignRecordEntity)
        {
            string sql = SqlCommands.GetInsertSql("VehicleIgnoreOrderAssignRecord", new string[] { "OrderId", "OrderNo", "VehicleNo", "DriverId" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderId", vehicleIgnoreOrderAssignRecordEntity.OrderId, DbType.Int64, size: 20);
            parameters.Add("?OrderNo", vehicleIgnoreOrderAssignRecordEntity.OrderNo, DbType.AnsiString, size: 50);
            parameters.Add("?VehicleNo", vehicleIgnoreOrderAssignRecordEntity.VehicleNo, DbType.AnsiString, size: 20);
            parameters.Add("?DriverId", vehicleIgnoreOrderAssignRecordEntity.DriverId, DbType.Int64, size: 20);
            sql = string.Format("{0} {1} WHERE VehicleIgnoreOrderAssignRecordId=LAST_INSERT_ID();", sql, QUERY);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<VehicleIgnoreOrderAssignRecordEntity>(sql, parameters,databaseContext.Transaction);
        }

        public async Task<(IEnumerable<VehicleIgnoreOrderAssignRecordEntity>, int)> Query(string vehicleNo, string orderNo, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(vehicleNo))
            {
                wheres.Add("VehicleNo=?VehicleNo");
                parameters.Add("?VehicleNo", vehicleNo, DbType.AnsiString, size: 20);
            }
            if (!string.IsNullOrWhiteSpace(orderNo))
            {
                wheres.Add("OrderNo=?OrderNo");
                parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            }

            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY CreateTime DESC LIMIT {2},{3};
                               SELECT Count(1) FROM VehicleIgnoreOrderAssignRecord {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex - 1) * pageSize,
                  pageSize);

            return await this.databaseContext.Slave.QueryPageableListAsync<VehicleIgnoreOrderAssignRecordEntity>(sql, parameters);
        }

        public int GetTodayIgnoreCount(long driverId)
        {
            string sql = @"SELECT COUNT(1) FROM VehicleIgnoreOrderAssignRecord WHERE DriverId=?DriverId AND DATE(CreateTime)=DATE(NOW())";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?DriverId", driverId, DbType.Int64, size: 20);
            return this.databaseContext.Slave.ExecuteScalarAsync<int>(sql, parameters).Result;
        }

        public async Task<int> UpdateVehicleNoByVehicleIdAsync(long driverId, string vehicleNo)
        {
            return await databaseContext.Master.ExecuteAsync("UPDATE VehicleIgnoreOrderAssignRecord SET VehicleNo = ?VehicleNo WHERE DriverId = ?DriverId", new { driverId, vehicleNo }, databaseContext.Transaction);
        }
    }
}
